package app.esb.cartoon;

import app.common.dao.Leo_can_any_relaBean;
import app.common.dao.Leo_can_firmBean;
import app.common.dao.Leo_can_firm_relaBean;
import app.common.dao.Leo_can_nameBean;
import app.common.dao.Leo_can_timeBean;
import app.common.dao.Leo_can_typBean;
import app.common.dao.Leo_can_typ_relaBean;
import app.esb.BaseExcutor;
import cn.lily.com.container.data.Record;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.lang.Basic;
import cn.lily.com.lang.BusinessDate;

/**
 * 动漫处理类
 * @author JiaJ
 * @date 2017年12月2日
 *
 */
public class CartoonExcutor extends BaseExcutor {
	
	 /**
	  * 插入名称
	  * @param name
	  * @param can_id
	  */
	 public static void insertCartoonNames(String names,String canID){
		 
		 if(names == null || "".equals(names)){
			 return;
		 }else{
			 
			 String[] nameAs = Basic.split(names, ",");
			 
			 for(String nameA:nameAs){
				 
				 String name_id = getDBSequence().getBussSeq("leo_can_name", 8);
				 
				 Leo_can_nameBean nameBean = new Leo_can_nameBean();
				 
				 nameBean.setCan_id(canID);
				 nameBean.setCan_name_id(name_id);
				 nameBean.setCan_name_fix(nameA);
				 nameBean.setCan_create_oper("9999");
				 nameBean.setCan_create_time(BusinessDate.getTodaytime());
				 
				 nameBean.insert();
				 
			 }
		 }
	
	 }
	 
	 /**
	  * 校验名称是否存在
	  * @param name
	  * @return
	  */
	 public static boolean checkCartoonName(Record record){
		 
		 //名称处理
		 String nameAll = record.getString("CAN_NAME");
		 String nameEng = record.getString("CAN_NAM_ENG");
		 String nameJap = record.getString("CAN_NAM_JAP");
		 
		 if(nameEng != null && !"".equals(nameEng)){
			 nameAll += ","+ nameEng;
		 }
		 
		 if(nameJap != null && !"".equals(nameJap)){
			 nameAll += ","+ nameJap;
		 }
		 
		 nameAll = "'" + nameAll.replaceAll(",", "','") + "'";
		 
		 DBManager dbManager = DBFactory.getActionDB();
		 
		 DataTable dt = dbManager.executeQueryTable("select count(*) ASCOUNT from ${schema}.LEO_CAN_NAME where CAN_NAME_FIX in ("+nameAll+") ");
		 
		 dt.next();
		 int count = dt.getInt("ASCOUNT");
		 	 
		 if(count > 0){
			 return true;
		 }
		 else{
			 return false;
		 }
		 
	 }
	 
	 /**
	  * 获取其他名称
	  * @param record
	  * @return
	  */
	 public static String getCartoonOtherName(Record record){
		 
		 //名称处理
		 String nameOther = "";
		 String nameEng = record.getString("CAN_NAM_ENG");
		 String nameJap = record.getString("CAN_NAM_JAP");
		 
		 if(nameEng != null && !"".equals(nameEng)){

			 nameOther = nameEng;
		 }
		 
		 if(nameJap != null && !"".equals(nameJap)){
			 if(!"".equals(nameOther)){
				 nameOther += "," + nameJap;
			 }else{
				 nameOther = nameJap;
			 }
		 }
		 
		 return nameOther;
	 }
	 
	 /**
	  * 获取总名称
	  * @param record
	  * @return
	  */
	 public static String getCartoonAllName(Record record){
		 
		 //名称处理
		 String nameAll = record.getString("CAN_NAME");
		 String nameEng = record.getString("CAN_NAM_ENG");
		 String nameJap = record.getString("CAN_NAM_JAP");
		 
		 if(nameEng != null && !"".equals(nameEng)){
			 nameAll += ","+ nameEng;
		 }
		 
		 if(nameJap != null && !"".equals(nameJap)){
			 nameAll += ","+ nameJap;
		 }
		 
		 return nameAll;
		 
	 }
	 
	/**
	 * 初始化Cartoon关联信息
	 * @param canID
	 */
	public static void instCartoonRela(String canID) {
		Leo_can_any_relaBean canRelaBean = new Leo_can_any_relaBean();
		canRelaBean.setCan_id(canID);
		canRelaBean.setCan_is_commic("0");
		canRelaBean.setCan_is_game("0");
		canRelaBean.setCan_is_music("0");
		canRelaBean.setCan_is_novel("0");
		canRelaBean.insert();
	}
	
	/**
	 * 插入动漫类型
	 * @param type
	 * @param canDetailID
	 */
	public static void insertCartoonType(String type,String canDetailID){	
	
		 if(type != null && !"".equals(type)){
			 
			 String[] types = Basic.split(type, ",","，");
			 
			 for(String active:types){
				 
				 active = active.trim();
				 
				 Leo_can_typ_relaBean typRelaBean = new Leo_can_typ_relaBean();
				 Leo_can_typBean typBean = new Leo_can_typBean();
				 
				 DataTable dt = DBFactory.getActionDB().executeQueryTable("select * from ${schema}.LEO_CAN_TYP where CAN_TYP_NAM='"+active+"'");
				 
				 int size = dt.getRecordCount();
				 
				 if(size == 0){
					 
					 String typ = getDBSequence().getBussSeq("leo_can_typ", 8);
					 typBean.setCan_typ_id(typ);
					 typBean.setCan_typ_nam(active);
					 typBean.setCan_create_oper("9999");
					 typBean.setCan_create_time(BusinessDate.getTodaytime());
					 typBean.insert();
					 
					 typRelaBean.setCan_typ_id(typ);
					 typRelaBean.setCan_detail_id(canDetailID);
					 typRelaBean.insert();
					 
				 }else{
					 
					 dt.next();
					 
					 typRelaBean.setCan_typ_id(dt.getString("CAN_TYP_ID"));
					 typRelaBean.setCan_detail_id(canDetailID);
					 typRelaBean.insert();
					 
				 }
				 
				 
			}
			
		 }
	
	 }
	
	/**
	 * 删除类型信息
	 * @param canDetailID
	 */
	public static void delCartoonType(String canDetailID){
		String sql = "delete from ${schema}.LEO_CAN_TYP_RELA where CAN_DETAIL_ID='"+canDetailID+"'";
		DBFactory.getActionDB().executeUpdate(sql);
	}
	
	/**
	 * 获取全部类型信息 - 中文
	 * @param canID
	 * @return
	 */
	public static String getCartoonTyps(String canID){
		String sql = "select * from ${schema}.LEO_CAN_DETAIL_INFO where CAN_ID='"+canID+"'";
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sql);
		
		if(dt.next()){
			return dt.getString("CAN_TYP_MEMO");
		}else{
			return "";
		}		
	}
	
	/**
	 * 插入厂商信息
	 * @param type
	 * @param canDetailID
	 */
	public static void insertCartoonFirms(String firms,String canDetailID ){	
	
		 if(firms != null && !"".equals(firms)){
			 
			 String[] firm = Basic.split(firms, ",","，");
			 
			 for(String active:firm){
				 
				 active = active.trim();
				 
				 Leo_can_firm_relaBean relaBean = new Leo_can_firm_relaBean();
				 Leo_can_firmBean firmBean = new Leo_can_firmBean();
				 
				 DataTable dt = DBFactory.getActionDB().executeQueryTable("select * from ${schema}.LEO_CAN_FIRM where CAN_FIRM_NAM='"+active+"'");
				 
				 int size = dt.getRecordCount();
				 
				 if(size == 0){
					 
					 String firm_id = getDBSequence().getBussSeq("leo_can_firm", 8);
					 firmBean.setCan_firm_id(firm_id);
					 firmBean.setCan_firm_nam(active);
					 firmBean.setCan_create_oper("9999");
					 firmBean.setCan_create_time(BusinessDate.getTodaytime());
					 firmBean.insert();
					 
					 relaBean.setCan_firm_id(firm_id);
					 relaBean.setCan_detail_id(canDetailID);
					 relaBean.insert();
					 
				 }else{
					 
					 dt.next();
					 
					 relaBean.setCan_firm_id(dt.getString("CAN_FIRM_ID"));
					 relaBean.setCan_detail_id(canDetailID);
					 relaBean.insert();
					 
				 }
				 
				 
			}
			
		 }
	
	 }
	
	/**
	 * 删除厂商信息
	 * @param canDetailID
	 */
	public static void delCartoonFirms(String canDetailID){
		String sql = "delete from ${schema}.LEO_CAN_FIRM_RELA where CAN_DETAIL_ID='"+canDetailID+"'";
		DBFactory.getActionDB().executeUpdate(sql);
	}
	
	/**
	 * 获取厂商名称
	 * @param canID
	 * @return
	 */
	public static String getCartoonFirms(String canID){
		String sql = "select * from ${schema}.LEO_CAN_DETAIL_INFO where CAN_ID='"+canID+"'";
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sql);
		
		if(dt.next()){
			return dt.getString("CAN_FIRM_MEMO");
		}else{
			return "";
		}		
	}
	
	/**
	 * 添加动漫时间信息
	 * @param time
	 * @param canDetailID
	 */
	public static void insertCartoonTime(String time,String canDetailID){
		
		 time = BusinessDate.formatDate(time);
		 if(time != null && !"".equals(time)){
			 String[] times = time.split("-");
			 String year = times[0];
			 String month = times[1];
			 
			 Leo_can_timeBean timeBean = new Leo_can_timeBean();
			 timeBean.setCan_detail_id(canDetailID);
			 
			 if("01,02,03".indexOf(month)>-1){
				 timeBean.setCan_fans(year+"01");
			 }
			 else if("04,05,06".indexOf(month)>-1){
				 timeBean.setCan_fans(year+"04");
			 }
			 else if("07,08,09".indexOf(month)>-1){
				 timeBean.setCan_fans(year+"07");
			 }
			 else if("10,11,12".indexOf(month)>-1){
				 timeBean.setCan_fans(year+"10");
			 }
			 	 
			 timeBean.setCan_year(year);
			 timeBean.setCan_month(month);
			 timeBean.insert();
		 }
		 
	}
	
	/**
	 * 删除动漫时间信息
	 * @param canDetailID
	 */
	public static void delCartoonTime(String canDetailID){
		String sql = "delete from ${schema}.LEO_CAN_TIME where CAN_DETAIL_ID='"+canDetailID+"'";
		DBFactory.getActionDB().executeUpdate(sql);
	}
	
	/**
	 * 获取动漫简介信息 - 主表
	 * @param canID
	 * @return
	 */
	public static String getCartoonMemos(String canID){
		String sql = "select * from ${schema}.LEO_CAN_INFO where CAN_ID='"+canID+"'";
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sql);
		
		if(dt.next()){
			return dt.getString("CAN_DEMO");
		}else{
			return "";
		}		
	}
	
}
